#ifndef __CTSBuilder_H__
#define __CTSBuilder_H__
#include "ITSBuilder.h"
#include "IESParser.h"
#include "CTS.h"
#include "CTimer.h"
#include <vector>
#include <map>
#include <set>

/** 
This class builds TS packets from the data that was read from the ES.(by CESParser)
When it finishes to build a TS it puts it its TS list (using PID to determine
the TS list it belongs to)
*/
class CTSBuilder : public ITSBuilder
{
public:
	/**
    c'tor
	*/
	CTSBuilder(std::set<unsigned int> pids, bool bUseTimer = true);
	/**
    d'tor
	*/
	virtual ~CTSBuilder();
    /**
	This function gets cESParser that holds the raw data it read from the binary file (ES stream)
	It goes through the data and builds TS packets from it.
	then when the new TS packet is ready - it puts it to TS list.
    @param cESParser- the CESParser class that holds the data read from the file (ES stream)
	*/
	virtual void findTS(IESParser* cESParser);

	/**
	Return a list of Tss that were read with a specific PID
	@param PID the pid of the wanted Tss
	@return a vector of TSs
	*/
	virtual std::vector<CTS*>* getTs(unsigned int PID);


	
private:
	/**
    goes through the array that keeps the data read from the file and builds TS packets
	from this data
	@return the number of bytes that were read
	*/
	int _BuildTS(const char* cBuffer, int nBufferSize);
    /**
    puts the new TS to its TS list.
	*/
	void _add2List();


	char m_sLastTs[188];
	int m_nCurrentTsSize;
	std::set<unsigned int> m_pids;
	std::map<unsigned int, std::vector<CTS*>*> m_mStreams;
	bool m_bUseTimer;
	CTimer m_cTimer;
};
#endif //__CTSBuilder_H__
